package com.szgr.baoyin.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.MyTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class InvoiceAddActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioButton rb_left;
    private RadioButton rb_right;
    private LinearLayout viewNormal;
    private LinearLayout viewValue;
    private EditText et_01;
    private EditText et_02;
    private EditText et_03;
    private EditText et_04;
    private EditText et_05;
    private EditText et_06;
    private EditText et_07;
    private EditText et_08;
    private EditText et_09;
    private EditText et_10;
    private String invoiceType = "2";
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 45:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_invoice_add);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("新增发票");
        actionBarConfirm.setVisibility(View.VISIBLE);
    }

    @Override
    public void initView() {
        RadioGroup rg_invoice = (RadioGroup) findViewById(R.id.invoice_radio_group);
        rb_left = (RadioButton) findViewById(R.id.invoice_first);
        rb_right = (RadioButton) findViewById(R.id.invoice_second);
        viewNormal = (LinearLayout) findViewById(R.id.normal_tip_message);
        viewValue = (LinearLayout) findViewById(R.id.register_tip_message);
        et_01 = (EditText) findViewById(R.id.invoice_01);
        et_02 = (EditText) findViewById(R.id.invoice_02);
        et_03 = (EditText) findViewById(R.id.invoice_03);
        et_04 = (EditText) findViewById(R.id.invoice_04);
        et_05 = (EditText) findViewById(R.id.invoice_05);
        et_06 = (EditText) findViewById(R.id.invoice_06);
        et_07 = (EditText) findViewById(R.id.invoice_07);
        et_08 = (EditText) findViewById(R.id.invoice_08);
        et_09 = (EditText) findViewById(R.id.invoice_09);
        et_10 = (EditText) findViewById(R.id.invoice_10);

//        rg_invoice.setOnCheckedChangeListener(this);
//        rb_left.setChecked(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.action_bar_confirm:
                addInvoice();
                break;
        }
    }

    private void addInvoice() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_ADD_INVOICE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("----->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            if(datas.has("inv_state") && datas.has("id")){
                                mHandler.obtainMessage(45).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("invoice_type",invoiceType);
                if("2".equals(invoiceType)) {
                    map.put("inv_company", MyTool.getText(et_01));
                    map.put("inv_code", MyTool.getText(et_02));
                    map.put("inv_reg_addr", MyTool.getText(et_03));
                    map.put("inv_reg_phone", MyTool.getText(et_04));
                    map.put("inv_reg_bname", MyTool.getText(et_05));
                    map.put("inv_reg_baccount", MyTool.getText(et_06));
                    map.put("inv_rec_name", MyTool.getText(et_07));
                    map.put("inv_rec_mobphone", MyTool.getText(et_08));
                    map.put("inv_rec_province", MyTool.getText(et_09));
                    map.put("inv_goto_addr", MyTool.getText(et_10));
                }else if("1".equals(invoiceType)){
                    map.put("inv_title_select","person");
                    map.put("inv_content","食品");
                }
                System.out.println("-----params---->" + map.toString());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.invoice_first:
                rb_left.setTextColor(0xffffffff);
                rb_right.setTextColor(0xff234b64);
                viewNormal.setVisibility(View.VISIBLE);
                viewValue.setVisibility(View.GONE);
                invoiceType = "1";
                break;
            case R.id.invoice_second:
                rb_right.setTextColor(0xffffffff);
                rb_left.setTextColor(0xff234b64);
                viewValue.setVisibility(View.VISIBLE);
                viewNormal.setVisibility(View.GONE);
                invoiceType = "2";
                break;
        }
    }
}
